package com.bw.shopping.mvvm.viewmodel;

import android.os.Bundle;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.goods.mvvm.model.entity.AddShopEntity;
import com.bw.mvvm_common.Code;
import com.bw.mvvm_common.dao.GoodBean;
import com.bw.mvvm_common.dao.GoodBeanDao;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.adapter.ShopCarAdapter;
import com.bw.shopping.entity.Shopcarentity;
import com.bw.shopping.entity.Subgoods;
import com.bw.shopping.entity.SubmitCar;
import com.bw.shopping.mvvm.model.ShopModel;
import com.bw.shopping.mvvm.view.fragment.OrderActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/20 0020 上午 10:47
 *
 */
class ShopViewModel extends BaseViewModel<ShopModel> implements ShopCarAdapter.shopMoney{
    public ObservableField<String> edtStr = new ObservableField<>("编辑");
    public ObservableField<BaseQuickAdapter> shopCarAdapter1 = new ObservableField<>();
    public ObservableField<String> allPrice= new ObservableField<>();


    int user_id;
    List<UserBean> list;
    List<GoodBean> goods;

    public  List<Shopcarentity.DataBean> data;

    int PRICE = 0;
    boolean isSelect = false;
    @Override
    public void onCreate() {
        super.onCreate();
        requestShopcar();
    }



    public void requestShopcar(){
        list = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list();
        if (list.isEmpty()){
            ToastUtils.showLong("未登录");
            HashMap<String,Object> map = new HashMap<>();
            map.put("id",10);
            m.requestShopcar(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        } else if (!list.isEmpty()){
            user_id = list.get(0).getUser_id();
            shopCarAdapter1.set(new ShopCarAdapter());
            ShopCarAdapter shopCarAdapter = (ShopCarAdapter) shopCarAdapter1.get();
            shopCarAdapter.setShopMoney(this);
            HashMap<String,Object> map = new HashMap<>();
            map.put("id",user_id);
            m.requestShopcar(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        }
        requestGoods();
    }
    public void requestGoods(){
        goods = GreenDaoUtils.getGreenDaoUtils().good().queryBuilder().list();
        for (GoodBean good : goods) {
            HashMap<String,Object> car = new HashMap<>();
            car.put("goodsCount",good.getGoodsCount());
            car.put("goodsDesc",good.getGoodsDesc());
            car.put("goodsIcon",good.getGoodsIcon());
            car.put("goodsId",good.getGoodsId());
            car.put("goodsPrice",good.getGoodsPrice());
            car.put("goodsSku",good.getGoodsSku());
            car.put("time",good.getTime());
            car.put("userId",user_id);
            m.requestadd(car)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }
        GoodBeanDao delgood = GreenDaoUtils.getGreenDaoUtils().good();
        delgood.deleteAll();

//        if (goods.isEmpty()&&!list.isEmpty()){
//            ToastUtils.showLong("同步购物车成功");
//        }
    }


    @Override
    protected void result(BaseEntity entity) {

        if (entity instanceof Shopcarentity){
            Shopcarentity shopcarentity = (Shopcarentity) entity;
            data = shopcarentity.getData();
            shopCarAdapter1.get().setNewInstance(data);

        }else if (entity instanceof AddShopEntity){
            AddShopEntity addShopEntity = (AddShopEntity) entity;
            if (addShopEntity.getMessage().equals("添加购物车成功")){

            }else {
                ToastUtils.showLong("sb");
            }
        }else if (entity instanceof SubmitCar){
            SubmitCar submitCar = (SubmitCar) entity;
            if (submitCar.getMessage().equals("购物车提交成功")){

                ToastUtils.showLong("提交成功");
                int data = submitCar.getData();
                Bundle bundle = new Bundle();
                bundle.putInt("orderid",data);
                HashMap<String,Object> map = new HashMap<>();
                map.put(UIField.PAGEBUNDLE,bundle);
                map.put(UIField.PAGECLAZZ, OrderActivity.class);
                startPage(map);
            }

        }

    }

    //编辑
    public void edtClickListener(){

    }
    //结算提交订单
    public void orderListener(){
        HashMap<String,Object> map = new HashMap<>();
        Subgoods subgoods = new Subgoods();
        ArrayList<Subgoods> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getFlag()==true){
                subgoods.setGoods_count(data.get(i).getGoods_count());
                subgoods.setGoods_desc(data.get(i).getGoods_desc());
                subgoods.setGoods_icon(data.get(i).getGoods_icon());
                subgoods.setGoods_id(data.get(i).getGoods_id());
                subgoods.setGoods_price(data.get(i).getGoods_price());
                subgoods.setGoods_sku(data.get(i).getGoods_sku());
                subgoods.setId(data.get(i).getId());
                subgoods.setOrder_id(0);
                list.add(subgoods);
            }
        }
        String s = allPrice.get();
        Integer integer = Integer.valueOf(s);
        LogUtils.e(list);

        map.put("goodsList",list);
        map.put("totalPrice",integer);
        map.put("userId",user_id);
        m.requestSubmitCar(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);



    }
    //全选
    public void selectAll(){
        PRICE = 0;
        if (isSelect){
            isSelect = false;
            for (Shopcarentity.DataBean datum : data) {
                datum.setFlag(false);
                allPrice.set("￥：0.00");
            }
        }else {
            isSelect = true;
            for (Shopcarentity.DataBean datum : data) {
                datum.setFlag(true);
                double price = Double.parseDouble(datum.getGoods_price());
                int num = datum.getGoods_count();
                PRICE+=price*num;
            }
        }

        shopCarAdapter1.get().notifyDataSetChanged();
        allPrice.set(PRICE+"");

    }


    @Override
    public void shopMoney(Boolean flag, Integer num, Integer position) {
        double prices;
        if (flag){
            PRICE = 0;
            for (Shopcarentity.DataBean datum : data) {
                if (datum.getFlag()){
                    double price = Double.parseDouble(datum.getGoods_price());
                    num = datum.getGoods_count();
                    PRICE += price * num;
                }
            }
        }else {

            prices = Double.parseDouble(data.get(position).getGoods_price());
            PRICE-= data.get(position).getGoods_count()*prices;
        }

        allPrice.set(PRICE+"");
        shopCarAdapter1.get().notifyItemChanged(position);
    }
}
